package com.example.producer;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import com.example.vo.DemoMessage;

import lombok.Data;

/**
 * 生产者
 */
@Data
@Component
@EnableScheduling
public class KafkaProducer {

	@Autowired
	private KafkaTemplate<String, String> kafkaTemplate;

	/**
	 * 消息主题
	 */
	private String topic = "test";

	/**
	 * 多条消息
	 */
	private ArrayList<String> msgs;

	public KafkaProducer(KafkaTemplate<String, String> kafkaTemplate) {
		this.kafkaTemplate = kafkaTemplate;

	}

	/**
	 * 向topic中发送多条消息
	 * 
	 * @param <T>
	 */
	public <T> void send(DemoMessage message) {
		if (message != null) {
			// kafkaTemplate.setDefaultTopic(topic);
			kafkaTemplate.send(topic, message.toString());
		} else {
			System.out.println("没有消息，发个毛线");
		}
//		if (msgs != null && msgs.size() > 0) {
//			msgs.forEach(msg -> kafkaTemplate.send(topic, msg));
//		} else {
//			System.out.println("没有消息，发个毛线");
//		}
	}

//    /**
//     * 向topic中发送单条消息
//     */
//    public void send(String topic, String msg) {
//        kafkaTemplate.send(topic, msg);
//    }
}
